<?php
require_once '../libs/app.config.php';
session_start();
if($_SESSION['LoginId'] == false){
	header("Location:login.php");
}
$tpn = new Page();
//分类列表
$sql="select id, orderId, origin, destination, items, status, createTime, completeTime from orders where status = 'completed' order by completeTime desc, orderId desc";
$query=$tpm->query($sql);
$totals=$tpm->num_rows($query);
	$tpn->pageft($totals,15);
	$result=$tpm->query($sql." limit ".$tpn->getfirstcount().",15");

while($rows=$tpm->fetch_array($result))
{
$order_list[]=array('id'=>$rows['id'],
				 'orderId'=>$rows['orderId'],
			     'origin'=>$rows['origin'],
				 'destination'=>$rows['destination'],
				 'items'=>$rows['items'],
				 'completeTime'=>$rows['completeTime']);
}

//多项删除
if($_POST['submit'])
{
	if(!empty($_POST['check']))
	{
		$array=$_POST['check'];
		$arr=implode(',',$array);
		$id=explode(',',$arr);
		$size=count($id);
		for($j=0;$j<$size;$j++)
		{
			$sql = "select distinct orderId from orders where id = '".$id[$j]."'";
			$query = $tpm->query ($sql);
			$rows = $tpm->fetch_array ( $query );
			$sql1="delete from orders where id= '".$id[$j]."'";
			$sql2="delete from order_records where orderId = '".$rows['orderId']."'";
			$query2=$tpm->query($sql2);
			$query1=$tpm->query($sql1);
			
			if($query2>0 && $query1>0)
			echo "<script>alert('删除成功');location.href=('complete_orders_list.php');</script>";
		}
	}
}

//单项删除
if($_POST["delid"])
{
  $sql = "select distinct orderId from orders where id = '".$_POST["delid"]."'";
  $query = $tpm->query ($sql);
  $rows = $tpm->fetch_array ( $query );
  $sql= "delete from orders where id= '".$_POST['delid']."'";
  $sql2="delete from order_records where orderId = '".$rows['orderId']."'";
  $query2=$tpm->query($sql2);
  $query1=$tpm->query($sql);  
  if($tpm->affected_rows($query2)&& $tpm->affected_rows($query1))
  echo "1";
  else
  echo "0";
}

if($_POST['rollbackId'])
{
	$sql= "update orders set status = 'ongoing', completeTime = NULL where id = '".$_POST['rollbackId']."'";
	$query=$tpm->query($sql);
	if($tpm->affected_rows($query))
		echo "1";
	else
		echo "0";
}

require_once(INCLUDE_PATH.'template.inc.php');  //模板类及对象$tpl的加载
    $tpl->assign("order_list",$order_list);
	$tpl->assign('pages',$tpn->getpagenav());
	$tpl->display("complete_orders_list.html");
?>